package com.dddd.blog.blogsp.pipeline;

import com.dddd.blog.blogsp.entity.Author;
import com.dddd.blog.blogsp.service.AuthorService;
import lombok.SneakyThrows;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.DisposableBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import us.codecraft.webmagic.ResultItems;
import us.codecraft.webmagic.Task;
import us.codecraft.webmagic.pipeline.Pipeline;

import java.io.*;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

/**
 * @author 喵星球的大橘
 */
@Component
public class BlogAuthorPipeline implements Pipeline, DisposableBean {

    @Autowired
    private AuthorService authorService;

    private static final String KEY_WORD_1 = "username";
    private static final String KEY_WORD_2 = "homeLink";

    private static BufferedReader br;
    private static BufferedWriter bw;
    private static File target;

    private static final String path = System.getProperty("user.dir")+"\\blog-sp\\src\\main\\resources\\Author.csv";

    static{
        target = new File(path);
    }

    @SneakyThrows
    @Override
    public void process(ResultItems resultItems, Task task){
        methodFirst(resultItems,task);
//        methodSecond(resultItems,task);
    }


    /**
     * 数据保存到数据库
     * @param resultItems
     * @param task
     */
    private void methodFirst(ResultItems resultItems, Task task){

        Map<String, Object> map = resultItems.getAll();

        if(map.get(KEY_WORD_1) == null || map.get(KEY_WORD_2) == null){
            return;
        }

        Author author = new Author();
        author.setUuid(UUID.randomUUID().toString());
        author.setUsername((String)map.get("username"));
        author.setNickname((String)map.get("username"));
        author.setHomeLink((String)map.get("homeLink"));
        author.setCreateTime(new Date());

        authorService.save(author);

    }

    /**
     * 将数据保存到文件
     * @param resultItems
     * @param task
     * @throws IOException
     */
    private void methodSecond(ResultItems resultItems, Task task) throws IOException {
        synchronized(this){

            if(!target.exists()){
                target.createNewFile();

            }
            Map<String, Object> map = resultItems.getAll();
            try {
                br = new BufferedReader(new FileReader(target));
                bw = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(target,true)));

                if(br.readLine() == null || br.readLine() == ""){
                    bw.write("#username\t#homeLink\n");
                    return;
                }

                if(map.get(KEY_WORD_1) == null || map.get(KEY_WORD_2)==null){
                    return;
                }

                bw.write(map.get("username")+","+map.get("homeLink")+"\n");

            } catch (IOException e) {
                e.printStackTrace();
            }finally {
                try {
                    bw.close();
                    br.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }



        }
    }



    @Override
    public void destroy(){

    }
}
